<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="广告收益" prop="adRevenue">-->
<!--        <el-input-->
<!--          v-model="queryParams.adRevenue"-->
<!--          placeholder="请输入广告收益"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="打赏金额" prop="revenueDonations">-->
<!--        <el-input-->
<!--          v-model="queryParams.revenueDonations"-->
<!--          placeholder="请输入打赏金额"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="总收益" prop="totalRevenue">-->
<!--        <el-input-->
<!--          v-model="queryParams.totalRevenue"-->
<!--          placeholder="请输入总收益"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="日期" prop="createdAt">
        <el-date-picker clearable
                        v-model="queryParams.createdAt"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:donations:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:donations:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:donations:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:donations:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 新增提现按钮 -->
    <el-button
      type="primary"
      icon="el-icon-money"
      size="mini"
      @click="showWithdrawDialog"
      style="float: right; margin-bottom: 10px;"
    >提现</el-button>

    <el-table v-loading="loading" :data="donationsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="revenueId" />
      <el-table-column label="广告收益" align="center" prop="adRevenue" />
      <el-table-column label="打赏金额" align="center" prop="revenueDonations" />
      <el-table-column label="总收益" align="center" prop="totalRevenue" />
      <el-table-column label="日期" align="center" prop="createdAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户收益对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- Your form fields go here -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 提现弹窗 -->
    <el-dialog :title="withdrawTitle" :visible.sync="withdrawDialogVisible" width="400px">
      <el-form>
        <el-form-item label="广告收益">
          <el-input v-model="withdrawForm.adRevenue" disabled />
        </el-form-item>
        <el-form-item label="打赏金额">
          <el-input v-model="withdrawForm.revenueDonations" disabled />
        </el-form-item>
        <el-form-item label="总收益">
          <el-input v-model="withdrawForm.totalRevenue" disabled />
        </el-form-item>
        <el-form-item label="提现方式">
          <el-radio-group v-model="withdrawForm.method">
            <el-radio label="微信">微信提现</el-radio>
            <el-radio label="支付宝">支付宝提现</el-radio>
            <el-radio label="京东E卡">京东E卡</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="withdrawDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleWithdraw">提 现</el-button>
      </div>
    </el-dialog>

    <!-- ECharts 容器 -->
    <div id="revenueChart" style="width: 100%; height: 400px;"></div>

  </div>
</template>

<script>
import * as echarts from "echarts";
import { listDonations, getDonations, delDonations, addDonations, updateDonations } from "@/api/system/donations";

export default {
  name: "Donations",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户收益表格数据
      donationsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        adRevenue: null,
        revenueDonations: null,
        totalRevenue: null,
        createdAt: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "关联的用户编号不能为空", trigger: "blur" }
        ],
        adRevenue: [
          { required: true, message: "广告收益不能为空", trigger: "blur" }
        ],
        revenueDonations: [
          { required: true, message: "打赏金额不能为空", trigger: "blur" }
        ],
        totalRevenue: [
          { required: true, message: "总收益不能为空", trigger: "blur" }
        ],
        createdAt: [
          { required: true, message: "日期不能为空", trigger: "blur" }
        ]
      },
      // 提现弹窗相关数据
      withdrawDialogVisible: false,
      withdrawTitle: "提现",
      withdrawForm: {
        adRevenue: 0,
        revenueDonations: 0,
        totalRevenue: 0,
        method: "微信"  // 默认选中微信
      }
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    this.renderChart();  // 在 mounted 钩子中初始化图表
  },
  methods: {
    /** 查询用户收益列表 */
    getList() {
      this.loading = true;
      listDonations(this.queryParams).then(response => {
        this.donationsList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.renderChart();  // 在每次数据获取之后重新渲染图表
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    // 新增按钮操作
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增用户收益";
    },
    // 修改按钮操作
    handleUpdate() {
      this.reset();
      const revenueId = this.ids;
      getDonations(revenueId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户收益";
      });
    },
    // 删除按钮操作
    handleDelete() {
      this.$confirm("是否确认删除用户收益编号为'" + this.ids + "'的数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        return delDonations(this.ids);
      }).then(() => {
        this.getList();
        this.$message.success("删除成功");
      });
    },
    /** 显示提现弹窗 */
    showWithdrawDialog() {
      const lastItem = this.donationsList[this.donationsList.length - 1] || {};
      this.withdrawForm.adRevenue = lastItem.adRevenue || 0;
      this.withdrawForm.revenueDonations = lastItem.revenueDonations || 0;
      this.withdrawForm.totalRevenue = lastItem.totalRevenue || 0;
      this.withdrawDialogVisible = true;
    },
    /** 处理提现操作 */
    handleWithdraw() {
      const method = this.withdrawForm.method;
      const adRevenue = this.withdrawForm.adRevenue;
      const revenueDonations = this.withdrawForm.revenueDonations;
      const totalRevenue = this.withdrawForm.totalRevenue;

      this.$message({
        message: `提现成功！提现方式：${method}，广告收益：${adRevenue}，打赏金额：${revenueDonations}，总收益：${totalRevenue}`,
        type: 'success'
      });

      this.withdrawDialogVisible = false;
    },
    /** 渲染 ECharts 图表 */
    renderChart() {
      const chartDom = document.getElementById('revenueChart');
      const myChart = echarts.init(chartDom);
      const option = {
        title: {
          text: '收益数据展示'
        },
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: ['广告收益', '打赏金额', '总收益']
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: this.donationsList.map(item => item.createdAt)
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            name: '广告收益',
            type: 'line',
            stack: 'Total',
            data: this.donationsList.map(item => item.adRevenue)
          },
          {
            name: '打赏金额',
            type: 'line',
            stack: 'Total',
            data: this.donationsList.map(item => item.revenueDonations)
          },
          {
            name: '总收益',
            type: 'line',
            stack: 'Total',
            data: this.donationsList.map(item => item.totalRevenue)
          }
        ]
      };
      myChart.setOption(option);
    }
  }
};
</script>

<style scoped>
/* 样式部分 */
</style>
